﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Windows.Forms;
using bll;
using System.Drawing;

namespace TimeIt.beispiele
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            bll.clsUserCollection _UserCol = new bll.clsUserCollection();
            bll.clsUser _clsIdUser = new bll.clsUser();
            Int32 sessionId = bll.clsUser.Session;
            if (sessionId == 0) { Response.Redirect("login.aspx"); }
            _clsIdUser = _UserCol.GetUserNameBySessionId();
            UserName.Text = _clsIdUser.Title + " " + _clsIdUser.Name;
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
                txtDate.Text = "";
                foreach (DateTime day in Calendar1.SelectedDates)
                {
                    txtDate.Text += day.Date.ToShortDateString();
                }
        }

        /// <summary>
        /// By click on the button the Timesheet will be added to the database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btninsert_time(object sender, System.EventArgs e)
        {
            //Delete warnings of fields (e. g. in case of page reload)
            dateError.Text = ""; txtDescriptionError.Text = ""; txtHoursError.Text = "";

            //Delivers the value to the bll!
            String[] msgFilled = new String[5];
            functionAdd add = new functionAdd();

            msgFilled = add.addTimeSheet(Convert.ToInt32(DropDownProject.Text),
                                         txtDate.Text,
                                         txtHours.Text,
                                         txtDescription.Text,
                                         checkChargeable.Checked,
                                         DropDownCategory.Text);

            checkMsg.Text = msgFilled[0];
            checkMsg.ForeColor = Color.FromArgb(Convert.ToInt32(msgFilled[1]));
            dateError.Text = msgFilled[2];
            txtHoursError.Text = msgFilled[3];
            txtDescriptionError.Text = msgFilled[4];

            //Update of the TimeSheet
            grdTimeSheet.DataBind();

        }//btninsert_time()

        protected void btninsert_employees(object sender, EventArgs e)
        {
            checkPMsg.Text = ""; errorMsgName.Text = ""; errorMsgEmail.Text = ""; 
            errorMsgHourlyRate.Text = ""; ErrorMsgPW.Text = "";

            String[] msgFilled = new String[5];
            functionAdd add = new functionAdd();
            msgFilled = add.addNewEmployee(Convert.ToString(DropDownTitle.Text),
                                           txtUName.Text,
                                           "Employee",
                                           txtUHourlyrate.Text,
                                           txtUPw.Text,
                                           txtUEmail.Text);

            checkPMsg.Text = msgFilled[0];
            checkPMsg.ForeColor = Color.FromArgb(Convert.ToInt32(msgFilled[1]));
            errorMsgName.Text = msgFilled[2];
            errorMsgEmail.Text = msgFilled[3];
            errorMsgHourlyRate.Text = msgFilled[4];
            ErrorMsgPW.Text = msgFilled[5];
            
            grd_employee.DataBind();

        }//btninsert_employees


        protected void projectMenu_MenuItemClick(object sender, MenuEventArgs e)
        {
            if (projectMenu.SelectedValue.Equals("first"))
                MultiView1.ActiveViewIndex = 0;
            if (projectMenu.SelectedValue.Equals("second"))
                MultiView1.ActiveViewIndex = 1;
            if (projectMenu.SelectedValue.Equals("third"))
                MultiView1.ActiveViewIndex = 2;
        }
    }
}